package com.tiancheng.trade.settlement.controller.admin;


import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BaseException;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.model.settlement.SettlementRule;
import com.tiancheng.trade.settlement.service.settlement.*;
import com.tiancheng.trade.settlement.vo.settlement.admin.*;
import com.tiancheng.trade.settlement.vo.settlement.admin.res.AbnormalDataResVO;
import com.tiancheng.trade.settlement.vo.settlement.api.GetBillDetailVO;
import com.tiancheng.trade.settlement.vo.settlement.api.GetSettlementRuleVO;
import com.tiancheng.trade.settlement.vo.settlement.api.SettlementBillDetailVO;
import com.tiancheng.trade.settlement.vo.settlement.api.SettlementBillRepVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * 清分对帐
 *
 * @author chris.shao
 * @since 2024-4-21
 */
@RestController
@RequestMapping("/admin/settlement/bill")
public class SettlementBillController {

    @Autowired
    private ISettlementBillResultService billResultService;

    @Autowired
    private ISettlementBillService settlementBillService;
    @Autowired
    private ISettlementBillDataService billDataService;
    @Autowired
    private ISettlementRuleService ruleService;

    @Autowired
    private ISettlementBillDataAdminService settlementBillDataAdminService;

    @Autowired
    private ISettlementBillQueryService settlementBillQueryService;

    /**
     * 清分异常处理 批量与单条匹配清分规则并对帐
     *
     * @param vo
     * @return
     * @throws BaseException
     */
    @PostMapping(value = "/matchingRule", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result matchingRule(@RequestBody @Valid BatchMatchingRuleVO vo) throws BaseException {
        SettlementRule rule = getSettlementRule(vo);
        if (rule == null) {
            return Result.fail("清分规则不存在");
        }
        return billDataService.matchingRule(vo.getSubOrderIds(), rule);
    }

    /**
     * 待对账单批量与单条匹配清分规则
     *
     * @param vo
     * @return
     * @throws BaseException
     */
    @PostMapping(value = "/waitBillMatchingRule", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result waitBillMatchingRule(@RequestBody @Valid BatchMatchingRuleVO vo) throws BaseException {
        SettlementRule rule = getSettlementRule(vo);
        if (rule == null) {
            return Result.fail("清分规则不存在");
        }
        return billDataService.waitBillMatchingRule(vo.getSubOrderIds(), rule);
    }

    private SettlementRule getSettlementRule(BatchMatchingRuleVO vo) {
        GetSettlementRuleVO ruleVO = new GetSettlementRuleVO();
        ruleVO.setRuleCode(vo.getRuleCode());
        List<SettlementRule> rules = ruleService.getSettlementRuleList(ruleVO);
        if (rules == null || rules.isEmpty()) {
            return null;
        }
        return rules.get(0);
    }

    /**
     * 清分对账汇总结果查询 -汇总统计列表
     *
     * @param billRepVO
     * @return
     * @throws BusinessException
     */
    @GetMapping("/summary/report")
    public Result querySummaryReport(SettlementBillQueryVO billRepVO) throws BusinessException {

        PageInfo<SettlementBillRepVO> list = billResultService.getSettlementBillReport(billRepVO);
        return Result.success(list);
    }

    /**
     * 清分汇总统计导出
     *
     * @param billRepVO
     * @return
     */
    @GetMapping("/summary/report/export")
    public Result querySummaryReportExport(SettlementBillQueryVO billRepVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        billResultService.exportSettlementBillReport(billRepVO, request, response);
        return Result.success("导出成功");
    }

    //清分对账-汇总对账 -列表
    @GetMapping("/summary/list")
    public Result querySummaryList(SettlementReconQueryVO billRepVO) throws BusinessException {
        PageInfo<SettlementReconRepVO> list = billResultService.getSettlementReconReport(billRepVO);
        return Result.success(list);
    }

    //清分对账-汇总对账 -导出
    @GetMapping("/summary/export")
    public Result querySummaryListExport(SettlementReconQueryVO billRepVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        billResultService.exportSettlementReconReport(billRepVO, request, response);
        return Result.success("导出成功");
    }

    //清分对账-明细对账查询-列表
    @GetMapping("/item/list")
    public Result queryItemList(GetBillDetailVO detailVO) throws BusinessException {
        PageInfo<SettlementBillDetailVO> list = billResultService.billDetailList(detailVO);
        return Result.success(list);
    }

    /**
     * 清分对账-明细对账导出
     *
     * @param billRepVO
     * @return
     */
    @GetMapping("/item/export")
    public Result queryItemListExport(GetBillDetailVO billRepVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        billResultService.exportSettlementBillDetail(billRepVO, request, response);
        return Result.success("导出成功");
    }

    // 清分待对帐单/待对帐异常-列表
    @GetMapping("/wait2Reconciled")
    public Result findPageList(SettlementBillDataQueryVO settlementBillDataQueryVO) {
        return Result.success(settlementBillDataAdminService.findPageList(settlementBillDataQueryVO));
    }

    // 清分异常批量查询-列表
    @GetMapping("/batchBill")
    public Result queryBatchBillList(SettlementBillDataQueryVO settlementBillDataQueryVO) {
        return Result.success(settlementBillDataAdminService.queryBatchBillList(settlementBillDataQueryVO));
    }

    // 清分待对帐单/待对帐异常-导出
    @GetMapping("/wait2Reconciled/export")
    public Result getExport(SettlementBillDataQueryVO settlementBillDataQueryVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        settlementBillDataAdminService.export(settlementBillDataQueryVO, request, response);
        return Result.success();
    }

    //    清分对账-清分异常数据查询-列表与详情（对账异常的数据）
    @GetMapping("/abnormalData")
    public Result queryAbnormalDataList(AbnormalDataQueryVO queryVO) throws BusinessException {
        return Result.success(billResultService.queryAbnormalDataList(queryVO));
    }

    /**
     * 清分对账明细详情
     *
     * @param subOrderId
     * @return
     */
    @GetMapping("/detail/{subOrderId}")
    public Result queryDetail(@PathVariable String subOrderId) {
        return Result.success(settlementBillQueryService.queryDetailBySubOrderId(subOrderId));
    }

    //    清分对账-清分异常数据单条备注与批量备注
    @PostMapping("/")
    public Result audit(@RequestBody SettlementBillHandleVO vo) throws BusinessException {
        return Result.success(settlementBillService.audit(vo));
    }

//    清分对账-清分异常数据单条处理与批量处理

    /**
     * 功能描述: 成功对账单
     *
     * @param:
     * @return:
     * @auther: LiuDong
     * @date: 17:57 2024/4/29
     */
    @GetMapping("/success")
    public Result querySuccessBill(SuccessBillQueryVO billRepVO) {
        return Result.success(billResultService.querySuccessBill(billRepVO));
    }

    /**
     * 功能描述:
     *
     * @param:
     * @return:
     * @auther: LiuDong
     * @date: 13:54 2024/4/30
     */
    @GetMapping("/success/export")
    public Result querySuccessBillexport(SuccessBillQueryVO billRepVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        billResultService.successBillexport(billRepVO, request, response);
        return Result.success();
    }

    /**
     * 按id查询结果信息
     *
     * @param ids
     * @return
     */
    @GetMapping("/getResultByIds")
    public Result getResultByIds(@RequestParam("list") List<String> ids) throws BusinessException {
        List<AbnormalDataResVO> result = billResultService.getResultByIds(ids);
        return Result.success(result);
    }


}
